import store from '@/store'
import { compareVersion } from '@/utils'
import { invokeAPi } from '@/xz-fn.js'
export function isPassVersion (ver) {
  const v1 = store.getters.appInfo.version
  return compareVersion(v1, ver) !== -1
}
export function isPassVersionByApp (obj) {
  const appId = store.getters.appInfo.appID
  const item = obj[appId]
  if (!item) return false
  if (!item.maxV) {
    return isPassVersion(item.v)
  } else {
    return isPassVersion(item.v) && !isPassVersion(item.maxV)
  }
}
export function filterAdData (cfg = {}) {
  const userInfo = store.getters.userInfo
  const hasLogin = userInfo.user_id
  const isMember = userInfo.is_member
  const now = Date.now()
  if (cfg.valid_date_start && now < new Date(cfg.valid_date_start).getTime()) return false
  if (cfg.valid_date_end && now > new Date(cfg.valid_date_end).getTime()) return false
  if (cfg.login === 0 && hasLogin) return false
  if (cfg.login === 1 && !hasLogin) return false
  if (cfg.user_type === 0 && isMember) return false
  if (cfg.user_type === 1 && !isMember) return false
  return true
}
export function openKefu () {
  const kefuUrlMap = {
    '22': 'https://feedback.xzdesktop.cqttech.com/feed',
    '108': 'https://xzsearch.cqttech.com/feedback.html',
    '111': 'https://xztodo.cqttech.com/feedback.html',
    '125': 'https://xzpaper.cqttech.com/feedback.html'
  }

  const appID = store.getters.appInfo.appID

  if (isPassVersionByApp({
    '22': { v: '3.2.0.1' },
    '144': { v: '1.0.0.0' }
  })
  ) {
    invokeAPi('xz_feedback')
  } else {
    invokeAPi('xz_open_url', kefuUrlMap[String(appID)])
  }
}
